// pages/address/add.js
var QQMapWX = require("../../utils/qqmap-wx-jssdk");//注意一定要相对路径，绝对路径可能会报错。
const api = require("../../request/api")
const request = require("../../request/request")
const util = require("../../utils/util")
const App = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    name_value: '',
    phone_value: '',
    region_value: '',
    address_value: '',
    latitude: 0,
    longtitude: 0,
    uid: '',
    default_value: false,
    options: {},
    current_local_address: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.init_data(options)
    this.get_location()

  },

  init_data(options) {
    this.setData({
      uid: util.getAppValue(App.globalData.uid),
      options
    })
  },

  use_current_local_address() {
    let current_local_address = this.data.current_local_address
    this.set_address_value(current_local_address.address, current_local_address.location.lng, current_local_address.location.lat)
  },

  set_address_value(address_value, longtitude, latitude) {
    this.setData({
      address_value,
      longtitude,
      latitude
    })
  },

  get_location() {
    let qqmapsdk = new QQMapWX({
      key: 'PFZBZ-CBHC2-E7EUP-CIGXG-5OZS5-UZF2Q'
    });
    let that = this
    wx.getLocation({
      success(res) {
        console.log(res);
        let latitude = res.latitude
        let longitude = res.longitude
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: latitude,
            longitude: longitude
          },
          success: function (res) {
            console.log(res)
            //如果本地存储没有城市信息，则将定位的城市信息存入本地存储
            that.setData({
              current_local_address: res.result
            })
            // wx.setStorageSync('city', city)
            //wx.setStorageSync('city_code', city_code)
          },
          fail: function (res) {
            console.log(res)
          }
        })
      }
    })
  },

  choose_location() {
    let that = this
    wx.chooseLocation({
      success: res => {
        console.log(res);
        that.set_address_value(res.address, res.longitude, res.latitude)
      }
    })
  },

  switching(e) {
    this.setData({
      default_value: e.detail.value
    })
  },

  picking_region(e) {
    this.setData({
      region_value: e.detail.value
    })
  },

  save_address() {
    if (this.data.name_value == '') {
      util.showToast('请输入您的姓名')
      return
    }
    if (this.data.phone_value == '') {
      util.showToast('请输入您的电话号码')
      return
    }
    if (this.data.region_value == '') {
      util.showToast('请输入您的所在地区')
      return
    }
    if (this.data.address_value == '') {
      util.showToast('请输入您的详细地址')
      return
    }
    let params = {
      uid: this.data.uid,
      name: this.data.name_value,
      mobile: this.data.phone_value,
      province_name: this.data.region_value[0],
      city_name: this.data.region_value[1],
      region_name: this.data.region_value[2],
      address: this.data.address_value,
      is_default: this.data.default_value ? 1 : 0
    }
    request.post(api.addressadd, params).then(res => {
      let pages = getCurrentPages()
      let prev_page = pages[pages.length - 2]
      prev_page.onLoad(this.data.options)
      util.goBack()
    })
  },

  wx_choose_address() {
    wx.chooseAddress({
      success: (result) => {
        let params = {
          uid: this.data.uid,
          name: result.userName,
          phone: result.telNumber,
          province_name: result.provinceName,
          city_name: result.cityName,
          region_name: result.countyName,
          detail: result.detailInfo,
          is_default: 0
        }
        request.post(api.addressadd, params).then(res => {
          let pages = getCurrentPages()
          let prev_page = pages[pages.length - 2]
          prev_page.onLoad(this.data.options)
          util.goBack()
        })
      },
    })
  },

  address_input(e) {
    this.setData({
      address_value: e.detail.value
    })
  },

  name_input(e) {
    this.setData({
      name_value: e.detail.value
    })
  },

  phone_input(e) {
    this.setData({
      phone_value: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})